package com.xiaoyuer.yygh.common.util;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class CopyUtis {
    public static <T, O> List<T> copy(Collection<O> origin, Class<T> c) {
        return copy(origin, c, (T e,O o)->{});
    }
    public static <T, O> List<T> copy(Collection<O> origin, Class<T> c, Handler<T, O> handler) {
        if (origin == null) {
            return Collections.EMPTY_LIST;
        }
        List<T> list = new ArrayList<>(origin.size());
        origin.forEach(e->list.add(copy(e, c, handler)));
        return list;
    }

    public static <T, O> T copy(O origin, Class<T> c, Handler<T, O> fun) {
        T dest = BeanUtils.instantiateClass(c);
        BeanUtils.copyProperties(origin, dest);
        if (fun != null) {
            fun.handle(dest, origin);
        }
        return dest;
    }

    public interface Handler<T, O> {
        void handle(T dest, O origin);
    }

}
